package gunfire.relics;

import basemod.abstracts.CustomRelic;
import com.badlogic.gdx.graphics.Texture;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.common.DrawCardAction;
import com.megacrit.cardcrawl.actions.common.RelicAboveCreatureAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.DexterityPower;
import com.megacrit.cardcrawl.powers.StrengthPower;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import gunfire.GunfireMod;
import gunfire.util.TextureLoader;

import static gunfire.GunfireMod.makeRelicOutlinePath;
import static gunfire.GunfireMod.makeRelicPath;

public class SoulOfOrb2 extends CustomRelic {

    public static final String ID = GunfireMod.makeID("SoulOfOrb2");
    private static final Texture IMG = TextureLoader.getTexture(makeRelicPath("SoulOfOrb2.png"));
    private static final Texture OUTLINE = TextureLoader.getTexture(makeRelicOutlinePath("blank.png"));

    public SoulOfOrb2() {
        super(ID, IMG, OUTLINE, RelicTier.SPECIAL, LandingSound.CLINK);
    }

    @Override
    public String getUpdatedDescription() {
        return DESCRIPTIONS[0];
    }

    @Override
    public void obtain() {
        if (AbstractDungeon.player.hasRelic("gunfire:SoulOfOrb")) {
            for(int i = 0; i < AbstractDungeon.player.relics.size(); ++i) {
                if (AbstractDungeon.player.relics.get(i).relicId.equals(SoulOfOrb.ID)) {
                    this.instantObtain(AbstractDungeon.player, i, true);
                    break;
                }
            }
        } else {
            super.obtain();
        }
    }

    @Override
    public void onEquip() {
        AbstractDungeon.player.energy.energy++;
        AbstractDungeon.player.energy.energyMaster++;
        if(AbstractDungeon.getCurrRoom().phase == AbstractRoom.RoomPhase.COMBAT){
            this.flash();
            this.addToBot(new ApplyPowerAction(AbstractDungeon.player, AbstractDungeon.player, new StrengthPower(AbstractDungeon.player, 1), 1));
            this.addToBot(new ApplyPowerAction(AbstractDungeon.player, AbstractDungeon.player, new DexterityPower(AbstractDungeon.player, 1), 1));
            for (AbstractCard c : AbstractDungeon.player.drawPile.group){
                if(c.hasTag(GunfireMod.ENERGY_ORB_CARD)){
                    c.modifyCostForCombat(-1);
                }
            }
            for (AbstractCard c : AbstractDungeon.player.hand.group){
                if(c.hasTag(GunfireMod.ENERGY_ORB_CARD)){
                    c.modifyCostForCombat(-1);
                }
            }
            for (AbstractCard c : AbstractDungeon.player.discardPile.group){
                if(c.hasTag(GunfireMod.ENERGY_ORB_CARD)){
                    c.modifyCostForCombat(-1);
                }
            }
            for (AbstractCard c : AbstractDungeon.player.exhaustPile.group){
                if(c.hasTag(GunfireMod.ENERGY_ORB_CARD)){
                    c.modifyCostForCombat(-1);
                }
            }
            this.addToBot(new RelicAboveCreatureAction(AbstractDungeon.player, this));
        }
    }

    @Override
    public void onUnequip() {
        AbstractDungeon.player.energy.energyMaster--;
    }

    @Override
    public void atBattleStart() {
        this.flash();
        this.addToBot(new ApplyPowerAction(AbstractDungeon.player, AbstractDungeon.player, new StrengthPower(AbstractDungeon.player, 2), 2));
        this.addToBot(new ApplyPowerAction(AbstractDungeon.player, AbstractDungeon.player, new DexterityPower(AbstractDungeon.player, 2), 2));
        for (AbstractCard c : AbstractDungeon.player.drawPile.group){
            if(c.hasTag(GunfireMod.ENERGY_ORB_CARD)){
                c.modifyCostForCombat(-1);
            }
        }
        this.addToBot(new RelicAboveCreatureAction(AbstractDungeon.player, this));
    }

    @Override
    public void onPlayCard(AbstractCard c, AbstractMonster m) {
        if(c.hasTag(GunfireMod.ENERGY_ORB_CARD) && AbstractDungeon.player.drawPile.group.size() > 0){
            this.addToBot(new DrawCardAction(1));
        }
    }

    @Override
    public AbstractRelic makeCopy() {
        return new SoulOfOrb2();
    }
}
